import { NameSpace } from 'utils';
import exceed from 'utils/apimap';

const ns = NameSpace('trashes');
export const RECEIVE_TRASHES_DATA = ns('RECEIVE_TRASHES_DATA');
export const LOADING = ns('LOADING');
export const UPDATE_DATA = ns('UPDATE_DATA');
export const RESET_DATA = ns('RESET_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

export function resetData() {
  return (dispatch) => {
    dispatch({
      type: RESET_DATA,
    });
  };
}

// 回收站的数据
export function getTrashes(page, pageSize) {
  return (dispatch) => {
    dispatch(
      updateData({
        isLoadingTable: true,
      }),
    );
    exceed
      .fetch({
        api: 'organization.trashes.list',
        data: {
          page,
          per_page: pageSize || 10,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_TRASHES_DATA,
          data: Object.assign(result, {
            currentPage: page,
          }),
        });

        dispatch(
          updateData({
            isLoadingTable: false,
          }),
        );
      })
      .catch(() => {
        dispatch(
          updateData({
            isLoadingTable: false,
            tableDataFetched: true,
          }),
        );
      });
  };
}
